package xupt.se.ttms.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xupt.se.ttms.dao.StudioDAO;
import xupt.se.ttms.model.Seat;
import xupt.se.ttms.model.Studio;

import com.google.gson.Gson;

@WebServlet("/seat/SeatServlet")
public class SeatServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException
    {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        int studio_id = Integer.parseInt(req.getParameter("studio_id"));
        StudioDAO studioDAO = new StudioDAO();
        ArrayList<Seat> list = studioDAO.findSeatById(studio_id);
        Studio studio = studioDAO.findStudioById(studio_id);

        // 插入座位
        /*
         * ArrayList<Studio> studios = studioDAO.findStudioAll(); SeatDAO seatDAO = new SeatDAO();
         * Seat seat = new Seat(); for(Studio s : studios) { seat.setStudio_id(s.getStudio_id());
         * int row_count = s.getStudio_row_count(); int col_count = s.getStudio_col_count(); for(int
         * i = 1; i <= row_count; i++) { for(int j = 1; j <= col_count; j++) { seat.setSeat_row(i);
         * seat.setSeat_column(j); if(studio.getStudio_flag() == 1) { try { seatDAO.insert(seat); }
         * catch(Exception e) { e.printStackTrace(); } } } } }
         */

        req.getSession().setAttribute("studio", studio);;
        Gson gson = new Gson();
        String json = gson.toJson(list);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        req.getSession().setAttribute("seat", json);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
}
